package com.ftcs.service.impl;

import com.ftcs.controller.common.request.StartWorkFlowRequest;
import com.ftcs.controller.common.request.TodoTasksRequest;
import com.ftcs.controller.common.service.ActivitiService;
import com.ftcs.entity.PurchaseApply;
import com.ftcs.mapper.PurchaseApplyMapper;
import com.ftcs.model.PurchaseTaskDTO;
import com.ftcs.model.TodoTaskDTO;
import com.ftcs.service.PurchaseService;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

//@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT,timeout=5)
@Service
@Slf4j
public class PurchaseServiceImpl implements PurchaseService {
    @Autowired
    PurchaseApplyMapper purchaseApplyMapper;
    @Autowired
    IdentityService identityservice;
    @Autowired
    RuntimeService runtimeservice;
    @Autowired
    TaskService taskservice;
    @Autowired
    private ActivitiService activitiService;

    @Override
    public ProcessInstance startWorkflow(PurchaseApply apply, String userid, String proDefName, Map<String, Object> variables) {
        purchaseApplyMapper.save(apply);
        //获取业务主键
        String businessKey = String.valueOf(apply.getId());
        log.info("bussinessKey:{}", businessKey);

        StartWorkFlowRequest request = StartWorkFlowRequest.builder().userId(userid).proDefName(proDefName).businesskey(businessKey).variables(variables).build();
        ProcessInstance processInstance = activitiService.startWorkFlow(request);

        apply.setProcessInstanceId(processInstance.getId());
        purchaseApplyMapper.updateByPrimaryKeySelective(apply);
        return processInstance;
    }

    @Override
    public PurchaseApply getPurchase(int id) {
        return purchaseApplyMapper.getPurchaseApply(id);
    }

    @Override
    public void updatePurchase(PurchaseApply a) {
        purchaseApplyMapper.updateByPrimaryKeySelective(a);
    }

    @Override
    public PurchaseTaskDTO listTodoTasks(TodoTasksRequest request) {
        TodoTaskDTO todoTaskDTO = activitiService.listTodoTasks(request);
//        List<PurchaseApply> purchaseApplyList = processData(todoTaskDTO.getTaskList());
        return PurchaseTaskDTO.builder().pageInfo(todoTaskDTO.getPageInfo()).taskList(todoTaskDTO.getTaskList()).build();
    }

//    private List<PurchaseApply> processData(List<Task> tasks) {
//        List<PurchaseApply> results = tasks.stream().map(task -> {
//            String businessKey = activitiService.getBusinessKeyByTaskId(task.getId());
//            PurchaseApply purchaseApply = getPurchase(Integer.parseInt(businessKey));
//            purchaseApply.setTask(task);
//            return purchaseApply;
//        }).collect(Collectors.toList());
//        return results;
//    }
}
